$(function() {
	initString();
	initDate();
	initUUID();
});

/**
 * 初始化字符串功能
 */
function initString() {

	// 判断是否为空字符串
	String.isEmpty = function(str) { // 这是一个静态方法
		if (str == null)
			return true;
		return /^\s*$/.test(str);
	};

	// ----以下是实例方法
	// 判断是否以指定字符串开始
	String.prototype.startWith = function(str) {
		var reg = new RegExp("^" + str);
		return reg.test(this);
	};
	// 判断是否以指定字符串结束
	String.prototype.endWith = function(str) {
		var reg = new RegExp(str + "$");
		return reg.test(this);
	};
	// 截取字符串前后空格
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, "");
	};
	// 判断是否整数
	String.prototype.isNumber = function() {
		if (String.isEmpty(this))
			return false;
		return /^\d+$/.test(this);
	};
	// 判断是否汉字
	String.prototype.isChinese = function() {
		if (String.isEmpty(this))
			return false;
		return /^[\u4E00-\u9FBF]+$/.test(this);
	};
	// 获取字符串的字节数
	String.prototype.getByteNum = function() {
		if (String.isEmpty(this))
			return 0;
		return this.replace(/[^\x00-\xff]/g, "**").length;
	};
}

/**
 * 初始化日期处理功能
 */
function initDate() {

	/**
	 * 初始化Date对象,为其增加pattern方法,使它具有转换为指定格式字符串的功能 用法:(new
	 * Date()).pattern("yyyy-MM-dd hh:mm:ss") ==> 2013-10-24 11:50:20
	 */
	Date.prototype.pattern = function(fmt) {
		var o = {
			"M+" : this.getMonth() + 1, // 月份
			"d+" : this.getDate(), // 日
			"h+" : this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
			"H+" : this.getHours(), // 小时
			"m+" : this.getMinutes(), // 分
			"s+" : this.getSeconds(), // 秒
			"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
			"S" : this.getMilliseconds()
		// 毫秒
		};
		var week = {
			"0" : "/u65e5",
			"1" : "/u4e00",
			"2" : "/u4e8c",
			"3" : "/u4e09",
			"4" : "/u56db",
			"5" : "/u4e94",
			"6" : "/u516d"
		};
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
					.substr(4 - RegExp.$1.length));
		}
		if (/(E+)/.test(fmt)) {
			fmt = fmt
					.replace(
							RegExp.$1,
							((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f"
									: "/u5468")
									: "")
									+ week[this.getDay() + ""]);
		}
		for ( var k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
						: (("00" + o[k]).substr(("" + o[k]).length)));
			}
		}
		return fmt;
	};

	/**
	 * 获取指定月份的最后一天
	 * 
	 * @param year
	 * @param month
	 * @returns
	 */
	Date.getLastDayInMonth = function(year, month) {
		month = parseInt(month, 10) + 1;
		var temp = new Date(year + "/" + month + "/0");
		return temp.getDate();
	};
}

function initUUID() {

	// Private array of chars to use
	var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
			.split('');
	Math.uuid = function(len, radix) {
		var chars = CHARS, uuid = [], i;
		radix = radix || chars.length;

		if (len) {
			// Compact form
			for (i = 0; i < len; i++)
				uuid[i] = chars[0 | Math.random() * radix];
		} else {
			// rfc4122, version 4 form
			var r;
			// rfc4122 requires these characters
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
			uuid[14] = '4';
			// Fill in random data. At i==19 set the high bits of clock sequence
			// as
			// per rfc4122, sec. 4.1.5
			for (i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | Math.random() * 16;
					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
				}
			}
		}
		return uuid.join('');
	};

	// A more performant, but slightly bulkier, RFC4122v4 solution. We boost
	// performance
	// by minimizing calls to random()
	Math.uuidFast = function() {
		var chars = CHARS, uuid = new Array(36), rnd = 0, r;
		for (var i = 0; i < 36; i++) {
			if (i == 8 || i == 13 || i == 18 || i == 23) {
				uuid[i] = '-';
			} else if (i == 14) {
				uuid[i] = '4';
			} else {
				if (rnd <= 0x02)
					rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
				r = rnd & 0xf;
				rnd = rnd >> 4;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
		return uuid.join('');
	};

	// A more compact, but less performant, RFC4122v4 solution:
	Math.uuidCompact = function() {
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
				function(c) {
					var r = Math.random() * 16 | 0, v = c == 'x' ? r
							: (r & 0x3 | 0x8);
					return v.toString(16);
				});
	};
}

function cookie(key,value,days){
	if(key && value && days){
		$.cookie(key,value,{expires:days,path:'/'});
		return;
	}
	if(key && value){
		$.cookie(key,value,{path:'/'});
		return;
	}
	if(key){
		var value = $.cookie(key);
		if(!value || typeof(value) == "undefined" || value==null){
			return "";
		}
		return value;
	}
}

function removeCookie(key){
	$.cookie(key,'',{expires:0,path:'/'});
	return;
}